﻿using System;
using System.Collections.Generic;
using UnityEngine;
namespace GxFramework
{
    public static class AssetLoadStatus
    {
        public const int Idle = 0;
        public const int Wait = 1;
        public const int Running = 2;
        public const int Success = 3;
        public const int Fail = 4;
    }
    public class AssetBundleManager:IAssetHandler
    {
        public readonly Type Type_AssetObject = typeof(UnityEngine.Object);

        AssetBundleManifest _abManifest;
        Dictionary<string, AssetBundleInfo> _abMap = new Dictionary<string, AssetBundleInfo>();
        AssetBundleLoader _abLoader = new AssetBundleLoader();//加载ab包
        AssetBundleAssetLoader _abAssetLoader = new AssetBundleAssetLoader();//加载ab包中的资源
        public AssetBundleManager()
        {
            _abLoader._abMgr = this;
            _abLoader._asstLoader = _abAssetLoader;
        }
        public void SetAssetBundleManifest(AssetBundleManifest abManifest)
        {
            _abManifest = abManifest;
        }
        public bool isAssetBundleManifestDone { get { return null != _abManifest; } }
        public string[] GetAllAssetBundles()
        {
            return isAssetBundleManifestDone ? _abManifest.GetAllAssetBundles():null;
        }
        public string[] GetAllAssetBundlesWithVariant()
        {
            return isAssetBundleManifestDone ? _abManifest.GetAllAssetBundlesWithVariant() : null;
        }
        public Hash128 GetAssetBundleHash(string assetBundleName)
        {
            return isAssetBundleManifestDone ? _abManifest.GetAssetBundleHash(assetBundleName) : default(Hash128);
        }
        public string[] GetAllDependencies(string assetBundleName)
        {
            return isAssetBundleManifestDone ? _abManifest.GetAllDependencies(assetBundleName) : null;
        }
        public string[] GetDirectDependencies(string assetBundleName)
        {
            return isAssetBundleManifestDone ?_abManifest.GetDirectDependencies(assetBundleName):null;
        }
        public AssetBundleInfo GetAssetBundleInfo(string bundlePath, bool autoCreate)
        {
            AssetBundleInfo info;
            if (!_abMap.TryGetValue(bundlePath, out info) && autoCreate)
            {
                _abMap[bundlePath] = info = new AssetBundleInfo();
                info.path = bundlePath;
            }
            return info;
        }
        
        public void LoadAsset(string bundlePath, string assetName, Type assetType, AssetDelegate del)
        {
            AssetBundleInfo info = GetAssetBundleInfo(bundlePath, true);
            _abLoader.Load(info);
            if (null == assetType)
                assetType = Type_AssetObject;
            var req = info.AddListener(assetName,assetType,del);
            if (info.isDone && !req.isDone)
                _abAssetLoader.Load(req);
        }
        public void LoadAllAssets(string bundlePath, Type assetType, AssetDelegate del)
        {
            AssetBundleInfo info = GetAssetBundleInfo(bundlePath, true);
            _abLoader.Load(info);
            if (null == assetType)
                assetType = Type_AssetObject;
            info.preloadAllAssets = true;
            var req = info.AddListener(null, assetType, del);
            if (info.isDone && !req.isDone)
                _abAssetLoader.Load(req);
        }
        public void Unload(string bundlePath)
        {

        }
        public void Update()
        {
            _abLoader.Update();
            _abAssetLoader.Update();
        }
    }
}
